package com.youlian.utils.bitmap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import com.youlian.utils.FileHelper;

public class BitmapHelper {

	private static final Config BITMAP_CONFIG  = Config.ARGB_8888;

	private Bitmap mBitmap;

	private CompressFormat format;

	private BitmapHelper(Bitmap bitmap) {
		mBitmap = bitmap;
	}

	/**
	 * 锟矫碉拷锟街伙拷SDcard目录锟铰碉拷图片
	 *
	 * @param context
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static BitmapHelper open(String filename) {
		byte[] data;
		try {
			data = FileHelper.openFile(filename);
		} catch (IOException e) {
			return new BitmapHelper(null);
		}
		return open(data);
	}

	public static BitmapHelper open(byte[] data) {
		Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
		return new BitmapHelper(bitmap);
	}

	public static Bitmap openBitmap(byte[] bytes){
		Bitmap bmp = BitmapUtils.getCorrectBmp(bytes, 2, Config.RGB_565);
		return bmp;
	}

	public static BitmapHelper open(InputStream is) throws IOException {
		Bitmap bitmap = BitmapFactory.decodeStream(is);
		is.close();
		return new BitmapHelper(bitmap);
	}

	public static BitmapHelper open(Context context,int resid) {
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resid);
		return new BitmapHelper(bitmap);
	}

	public static BitmapHelper open(Bitmap bitmap){
		return new BitmapHelper(bitmap);
	}

	/**
	 * 圆锟斤拷
	 * @param roundPx 圆锟斤拷锟斤拷锟斤拷
	 * @return
	 */
	public BitmapHelper corner(float roundPx) {

		if(isEmpty()) return this;

		int width = (int)(mBitmap.getWidth());
		int height = (int)(mBitmap.getHeight());

	    Bitmap output = Bitmap.createBitmap(width,
	    		height, BITMAP_CONFIG);
	    Canvas canvas = new Canvas(output);

	    final int color = 0xff000000;
	    final Paint paint = new Paint();
	    final Rect rect = new Rect(0, 0, width, height);
	    final RectF rectF = new RectF(0, 0, width, height);

	    paint.setAntiAlias(true);
	    canvas.drawARGB(0, 0, 0, 0);
	    paint.setColor(color);
	    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

//	    draw锟侥凤拷式锟斤拷锟斤拷
	    canvas.drawBitmap(mBitmap, rect, rectF, paint);

	    mBitmap = output;

	    return this;
	}

	/**
	 * 锟斤拷锟斤拷
	 *
	 * @param bitmap
	 * @param scale
	 * @return
	 */
	public BitmapHelper scale(float scale){

		if(isEmpty()) return this;

		Matrix matrix=new Matrix();
		matrix.postScale(scale, scale);
		Bitmap output=Bitmap.createBitmap(mBitmap,0,0,mBitmap.getWidth(),
				mBitmap.getHeight(),matrix,true);

	    mBitmap = output;

	    return this;
	}


	/***
	 * 锟矫碉拷图片
	 * @return Bitmap
	 */
	public Bitmap get(){

		return mBitmap;
	}

	public boolean isEmpty(){
		if(mBitmap==null){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 锟斤拷锟斤拷图片锟斤拷指锟斤拷位锟斤拷
	 *
	 * @param context
	 * @param bmp
	 * @param fileName
	 * @return
	 */
	public boolean save(String filename)
	{
		if(isEmpty()) return false;
		File file = FileHelper.getSDCardFile(filename);
		if(file != null){
			try {
				file.getParentFile().mkdirs();
				if(file.exists())file.delete();
				FileOutputStream os = new FileOutputStream(file);
				format = getFormatBySuffix(FileHelper.getSuffix(filename));
				mBitmap.compress(format, 50, os);
				os.flush();
				os.close();
				return true;
			} catch (Exception e) {
				return false;
			}
		}else{

			return false;
		}
	}
	public boolean save(Context context,String filename)
	{
		if(isEmpty()) return false;

		try {
			FileOutputStream os = context.openFileOutput(filename, Context.MODE_PRIVATE);
			format = getFormatBySuffix(FileHelper.getSuffix(filename));
			mBitmap.compress(format,50, os);
			os.flush();
			os.close();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void recycle(){
		mBitmap.recycle();
	}

	public CompressFormat getFormatBySuffix(String suffix){
		if(FileHelper.SUFFIX.JPG.equals(suffix)) return Bitmap.CompressFormat.JPEG;
		return Bitmap.CompressFormat.PNG;
	}
}
